package tech.aistar.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import tech.aistar.entity.Classs;
import tech.aistar.entity.Post;
import tech.aistar.entity.PostVo;
import tech.aistar.entity.Theme;
import tech.aistar.service.*;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/10/11.
 */
@Controller
@RequestMapping("/themeF")
public class ThemeActoin {
    @Autowired
    private IThemeService themeService;
    @Autowired
    private IClassService classService;
    @Autowired
    private IPostService postService;
    @Autowired
    private IComService comService;

    /**
     * 进入吧内
     */
    @RequestMapping("/themes")
    public String themes(Model model, Integer themeId){
        Theme themeT = themeService.findThemeById(themeId);
//        System.err.println(themeT+"111111111111111111111111111111111111111111");
        model.addAttribute("themeT",themeT);

        List<Classs> classsList = classService.findAllClass();
        model.addAttribute("classsList",classsList);

//        Long comN = comService.findOneComNum();

        //主题对应帖子数(暂时这么查)
        Long postNum = postService.findPostNumByTheme(themeId);
        model.addAttribute("postNum",postNum);
        //主题对应评论数
        Long comNum = comService.findComNumByTheme(themeId);
        model.addAttribute("comNum",comNum);
        return "/forum/theme";
    }


    /**
     * 吧内帖子分页
     */
    @RequestMapping("/postPage")
    public String postPage(Integer themeId,Model model,@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "8") Integer pageSize){

//        themeId = 1;
//        System.err.println("fhgjjjjjjjjjjjjjjjjjjjj");
        //调用service进行分页数据的获取
        Map<String,Object> map = postService.findPostByTheme(themeId,pageNum,pageSize);

        PageInfo<Post> pageInfo = (PageInfo<Post>) map.get("pageInfo");
        List<PostVo> postVos = (List<PostVo>) map.get("postVos");
        //将获取的数据保存到作用域中
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("postVos",postVos);
//        System.err.println(pageInfo);
//        System.err.println(postVos);

        //之前写的
//        List<PostVo> postList = postService.findPostByTheme(themeId);
//        model.addAttribute("postList",postList);
        return "/forum/themePageData";
    }


    /**
     * 进入发帖界面
     */
    @RequestMapping("/sendP")
    public String sendPost(Model model){
        List<Theme> themeAll = themeService.findAllTheme();
        model.addAttribute("themeAll",themeAll);

        List<Classs> classses = classService.findAllClass();
        model.addAttribute("classses",classses);
        return "/forum/send";
    }
}
